package com.sinven.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;


import com.sinven.pojo.LevelOneMenu;
import com.sinven.pojo.LevelTwoMenu;
import javafx.scene.control.Menu;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;


import org.springframework.stereotype.Controller;

import java.awt.*;
import java.io.IOException;


@Controller
@Scope(value = "prototype")
@ParentPackage(value = "json-default")
public class getAccess_token extends ActionSupport{
    private  static  String access_token="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=24_U2UN2C8ONGpSZ-QmfrHuLnkYDjzOi8IqOFKw3DAxjxqrAhRJSeiWrYDy678ksWBQ5di2PevAaiVVvKG94VoefGwilbpLE56zeyyYgwdCeUdc9FFDcpCfRJBm_B62ur6YCBOL_cF5zNPFUGdaGOXcAEAUCL";
    //"access_token":"24_kYv4aKq5FuK9400sWSu8oRQc4p7BuDAebCLs1Rf1vVwNUcfy3aX0Pm6zMWqMmqnF_1p4NNyL8JG32dXifIdbGWCbQMbsyCXNabad1U0aWd0z
    private final  static String APPID="wx05958fd0db70771d";
    private final  static String APPSECRET="f4b7afd607a4d8e8b9a63d4282beebb5";
    private  static   String  url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    /**
     * 获取access_token
     * @return
     * @throws IOException
     */
    @Action(value = "wy" )
     public  String executeToken() throws IOException {
        //112.74.89.58//c872be06b0f13c38b27f931200613dc0
            //创建一个请求客户端，模拟浏览器
         CloseableHttpClient client = HttpClientBuilder.create().build();

         url=  url.replace("APPID",APPID).replace("APPSECRET",APPSECRET);
         HttpGet httpGet=new HttpGet(url);
         CloseableHttpResponse response=client.execute(httpGet);
         String str=  EntityUtils.toString ( response.getEntity () ) ;
         System.out.println (str);
         client.close ();
         return NONE;
     }

    /**
     * 定义菜单
     * @return
     */
    @Action(value = "defineMenus")
     public String defineMenus() throws IOException {
         //创建一个请求客户端，模拟浏览器
         CloseableHttpClient client = HttpClientBuilder.create().build();
         HttpPost post=new HttpPost(access_token);
         LevelTwoMenu l21=new LevelTwoMenu ("view","搜索","","http://www.soso.com/" );
         LevelTwoMenu l22=new LevelTwoMenu ("miniprogram","wxa","",
                 "http://mp.weixin.qq.com" ,"wx286b93c14bbf93aa","pages/lunar/index");
         LevelTwoMenu l23=new LevelTwoMenu ("click","赞一下我们","V1001_GOOD","" );
         LevelOneMenu l1=new LevelOneMenu ( "click","今日歌曲","V1001_TODAY_MUSIC" );
         LevelOneMenu l2=new LevelOneMenu ( "","菜单","" );
         l2.setSub_button (new LevelTwoMenu[]{l21,l22,l23});
         com.sinven.pojo.Menu m=new com.sinven.pojo.Menu ();
         m.setButton ( new LevelOneMenu[]{l1,l2} );
         String json= JSON.toJSONString ( m );
         System.out.println (json);
        HttpEntity entity=new StringEntity ( json, ContentType.APPLICATION_JSON);
       post.setEntity (  entity);

       client.execute ( post );

         return  NONE;
     }

}
